package com.me.supermario.entities;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

@SuppressWarnings("unused")
public class Enemy extends Sprite {
	
	//IMPORTANTI
	private Vector2 velocity = new Vector2 ();
	private Rectangle rect = new Rectangle ();
	
	//COLLISIONLAYER
	private TiledMapTileLayer collisionLayer;
	
	//ANIMAZIONI E TEXTURE
	private static TextureAtlas enemyAtlas;
	private static Animation still;
	private static Animation left;
	private static Animation right;
	private static Animation dead;
	
	//BOOLEAN VARI
	private boolean deadState;
	private boolean firstTime;
	
	//INT
	private int gravity;
	private int animationTime;
	
	//FLOAT
	private float increment;
	private float speed;
	private float oldSpeed;
	
	//STRING
	private String blockedKey = "enemyblock";
	
	public Enemy(TiledMapTileLayer collisionLayer){
		super(still.getKeyFrame(0));
		this.collisionLayer = collisionLayer;
		
		still.setPlayMode(Animation.LOOP);
		left.setPlayMode(Animation.LOOP);
		right.setPlayMode(Animation.LOOP);
	}
	
	
	private void update(float delta) {
		velocity.y -= gravity*delta;
		if (firstTime){
			velocity.x = -speed;
			firstTime=false;
		} else {
			velocity.x = oldSpeed; 
		}

		if (deadState)
			velocity.set(0,0);
		if(velocity.y > speed)
			velocity.y = speed; //la velocita' non puo' andare oltre a speed
		else if(velocity.y < -speed)
			velocity.y = -speed;
		
		//salva le vecchie posizioni
		//float oldX = getX(); 
		float oldY = getY();
		//settiamo due valori booleani: uno per vedere se collidiamo nell'asse X, uno per l'asse Y
		//boolean collisionX = false; 
		boolean collisionY = false;

		// muovi in x
		setX(getX() + velocity.x * delta);

		// calculate the increment for step in #collidesLeft() and #collidesRight()
		increment = collisionLayer.getTileWidth();
		increment = getWidth() < increment ? getWidth() / 2 : increment / 2;

		// muovi in Y
		setY(getY() + velocity.y * delta * 5f);

		// calculate the increment for step in #collidesBottom() and #collidesTop()
		increment = collisionLayer.getTileHeight();
		increment = getHeight() < increment ? getHeight() / 2 : increment / 2;

		if(velocity.y < 0){ // stai andando giu'
			collisionY = collidesBottom();

		}
		else if(velocity.y > 0) // stai andando su
			collisionY = collidesTop();

		// gestisci la collisione in Y
		if(collisionY) {
			setY(oldY);
			velocity.y = 0;
		}

		boolean pathX = false;
		
		if(velocity.x < 0) // stai andando a sinistra
			pathX = collidesLeft();
		else if(velocity.x > 0) // stai andando a destra
			pathX = collidesRight();
		
		if (pathX){
			//setX(oldX - velocity.x*delta);
			//cambiadirezione
			//setX(oldX);
			if (velocity.x < 0){
			velocity.x = speed;
			this.oldSpeed = velocity.x;
			setX(getX() + velocity.x * delta);
			//System.out.println (velocity.x);
			}
			else if (velocity.x > 0){
				velocity.x = -speed;
				this.oldSpeed = velocity.x;
				setX(getX() + velocity.x * delta);
				//System.out.println (velocity.x);
				}
		}
		
		//aggiorna l'animazione
		animationTime += delta;
		setRegion(velocity.x < 0 ? left.getKeyFrame(animationTime) : velocity.x > 0 ? right.getKeyFrame(animationTime) : getDeadState()? dead.getKeyFrame(animationTime) : still.getKeyFrame(animationTime));
	
		this.oldSpeed = velocity.x;
		//System.out.println ("FINE: Velocita' = " + velocity.x);
	}//update

	
	public boolean collidesRight() {
		for(float step = 0; step < getHeight(); step += 8)
			if(isCellEnemyBlocked(getX() + getWidth(), getY() + step )){
				return true;
			}
		return false;
	}//collidesRight

	public boolean collidesLeft() {
		for(float step = 0; step < getHeight(); step += 8)
			if(isCellEnemyBlocked(getX() , getY() + step  )){
				return true;
		}
		return false;
	}//collidesLeft

	public boolean collidesTop() {
		for(float step = 0; step < getWidth(); step += 8)
			if(isCellEnemyBlocked(getX() + step, getY() + getHeight())){
				return true;
			}
		return false;
	}//collidesTop

	public boolean collidesBottom() {
		for(float step = 0; step < getWidth(); step += 8)
			if(isCellEnemyBlocked(getX()+step, getY() )){
				return true;
			}		
		return false;
	}//collidesBottom
	
	private boolean isCellEnemyBlocked(float x, float y) {
		Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
		return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
	}//isCellBlocked
	
	public boolean getDeadState() {
		return deadState;
	}//getDeadState


	public void setDeadState(boolean deadState) {
		this.deadState = deadState;
	}//setDeadState
	

}
